﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LyLichSinhVienService;
namespace eTMS.Controllers{
public class LyLichSinhVienControllerBase : CommonController{
	public LyLichSinhVienControllerBase()
		: base()
	{	
		this.dao = new LyLichSinhVienServiceClient();
	}
	~LyLichSinhVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LyLichSinhVienEntity oLyLichSinhVien;	
	public LyLichSinhVienEntity LyLichSinhVienObject{
		get{
			return oLyLichSinhVien;
		}
		set{
			oLyLichSinhVien=value;
		}
	}
	protected LyLichSinhVienServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSLyLichSinhVien(false);			
			return _Table;
		}
	}
	public void LoadAllDSLyLichSinhVien(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLyLichSinhVien==null)return false;
		bool result=true;
		try{
			oLyLichSinhVien = dao.SaveRet(oLyLichSinhVien);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLyLichSinhVien.Add(oLyLichSinhVien);
		}
		return result;
	}
	public bool Update(){
		if(oLyLichSinhVien==null)return false;
		return dao.Update(oLyLichSinhVien);
	}
	public bool Delete(){
		if(oLyLichSinhVien==null)return false;
		bool retvalue = dao.Delete(oLyLichSinhVien);
		if(retvalue){			
			_ListLyLichSinhVien.Remove(oLyLichSinhVien);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 SinhVien_ID){
		bool retvalue = dao.DeleteByID(SinhVien_ID);		
		if(retvalue){
			LyLichSinhVienEntity oDeleted=FindLyLichSinhVien(SinhVien_ID);
			if(oDeleted!=null){
				_ListLyLichSinhVien.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLyLichSinhVien = new LyLichSinhVienEntity();
	}
    //Copy data from data row in grid to object
    public LyLichSinhVienEntity MapDataRowToObject(DataRow row){
		LyLichSinhVienEntity o = new LyLichSinhVienEntity();
		 o.TruongTuyenSinh=Convert.ToString(row["TruongTuyenSinh"]);
		 o.DiaChiThuongTru=Convert.ToString(row["DiaChiThuongTru"]);
		 o.DiaChiHienTai=Convert.ToString(row["DiaChiHienTai"]);
		 o.MaNganhDuTuyen=Convert.ToString(row["MaNganhDuTuyen"]);
		 o.SoBaoDanh=Convert.ToString(row["SoBaoDanh"]);
		 if(!row.IsNull("DiemDuTuyen"))
			o.DiemDuTuyen=ConvertNumber(row["DiemDuTuyen"]);
		 if(!row.IsNull("DiemThuong"))
			o.DiemThuong=ConvertNumber(row["DiemThuong"]);
		 o.Lop10=Convert.ToString(row["Lop10"]);
		 o.Lop11=Convert.ToString(row["Lop11"]);
		 o.Lop12=Convert.ToString(row["Lop12"]);
		 if(!row.IsNull("NamTotNghiepPTTH"))
			o.NamTotNghiepPTTH=ConvertInt(row["NamTotNghiepPTTH"]);
		 o.KhenThuongKyLuat=Convert.ToString(row["KhenThuongKyLuat"]);
		 o.TomTatQuaTrinhHocTap=Convert.ToString(row["TomTatQuaTrinhHocTap"]);
		 o.NgayVaoDoan=Convert.ToString(row["NgayVaoDoan"]);
		 o.NoiVaoDoan=Convert.ToString(row["NoiVaoDoan"]);
		 o.NgayVaoDang=Convert.ToString(row["NgayVaoDang"]);
		 o.NoiVaoDao=Convert.ToString(row["NoiVaoDao"]);
		 o.BaoTin=Convert.ToString(row["BaoTin"]);
		 o.HoTenCha=Convert.ToString(row["HoTenCha"]);
		 o.NgaySinhCha=Convert.ToString(row["NgaySinhCha"]);
		 o.NgheNghiepCha=Convert.ToString(row["NgheNghiepCha"]);
		 o.HoTenMe=Convert.ToString(row["HoTenMe"]);
		 o.NgaySinhMe=Convert.ToString(row["NgaySinhMe"]);
		 o.NgheNghiepMe=Convert.ToString(row["NgheNghiepMe"]);
		 o.AnhChiEmRuot=Convert.ToString(row["AnhChiEmRuot"]);
		 o.VoHoacChong=Convert.ToString(row["VoHoacChong"]);
		 if(!row.IsNull("DanToc_ID"))
			o.DanToc_ID=(Int32)(row["DanToc_ID"]);
		 if(!row.IsNull("DoiTuongTS_ID"))
			o.DoiTuongTS_ID=(Int32)(row["DoiTuongTS_ID"]);
		 if(!row.IsNull("KhuVuc_ID"))
			o.KhuVuc_ID=(Int32)(row["KhuVuc_ID"]);
		 if(!row.IsNull("QuanHuyen_ID"))
			o.QuanHuyen_ID=(Int32)(row["QuanHuyen_ID"]);
		 if(!row.IsNull("QuocGia_ID"))
			o.QuocGia_ID=(Int32)(row["QuocGia_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 if(!row.IsNull("TinhThanh_ID"))
			o.TinhThanh_ID=(Int32)(row["TinhThanh_ID"]);
		 if(!row.IsNull("TonGiao_ID"))
			o.TonGiao_ID=(Int32)(row["TonGiao_ID"]);
		 if(!row.IsNull("XepLoaiPhoThong_ID"))
			o.XepLoaiPhoThong_ID=(Int32)(row["XepLoaiPhoThong_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
		    o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LyLichSinhVienEntity o,DataRow row){
		row["TruongTuyenSinh"]=o.TruongTuyenSinh;
		row["DiaChiThuongTru"]=o.DiaChiThuongTru;
		row["DiaChiHienTai"]=o.DiaChiHienTai;
		row["MaNganhDuTuyen"]=o.MaNganhDuTuyen;
		row["SoBaoDanh"]=o.SoBaoDanh;
		row["DiemDuTuyen"]=o.DiemDuTuyen;
		row["DiemThuong"]=o.DiemThuong;
		row["Lop10"]=o.Lop10;
		row["Lop11"]=o.Lop11;
		row["Lop12"]=o.Lop12;
		row["NamTotNghiepPTTH"]=o.NamTotNghiepPTTH;
		row["KhenThuongKyLuat"]=o.KhenThuongKyLuat;
		row["TomTatQuaTrinhHocTap"]=o.TomTatQuaTrinhHocTap;
		row["NgayVaoDoan"]=o.NgayVaoDoan;
		row["NoiVaoDoan"]=o.NoiVaoDoan;
		row["NgayVaoDang"]=o.NgayVaoDang;
		row["NoiVaoDao"]=o.NoiVaoDao;
		row["BaoTin"]=o.BaoTin;
		row["HoTenCha"]=o.HoTenCha;
		row["NgaySinhCha"]=o.NgaySinhCha;
		row["NgheNghiepCha"]=o.NgheNghiepCha;
		row["HoTenMe"]=o.HoTenMe;
		row["NgaySinhMe"]=o.NgaySinhMe;
		row["NgheNghiepMe"]=o.NgheNghiepMe;
		row["AnhChiEmRuot"]=o.AnhChiEmRuot;
		row["VoHoacChong"]=o.VoHoacChong;
		row["DanToc_ID"]=o.DanToc_ID;
		row["DoiTuongTS_ID"]=o.DoiTuongTS_ID;
		row["KhuVuc_ID"]=o.KhuVuc_ID;
		row["QuanHuyen_ID"]=o.QuanHuyen_ID;
		row["QuocGia_ID"]=o.QuocGia_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["TinhThanh_ID"]=o.TinhThanh_ID;
		row["TonGiao_ID"]=o.TonGiao_ID;
		row["XepLoaiPhoThong_ID"]=o.XepLoaiPhoThong_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LyLichSinhVienObject,row);
    }
	public void LoadObject(Int32 SinhVien_ID){
		LyLichSinhVienEntity oLoad=FindLyLichSinhVien(SinhVien_ID);
		if(oLoad==null){
			oLyLichSinhVien=this.LoadLyLichSinhVien(SinhVien_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLyLichSinhVien.IsNullSinhVien_ID;
			if(!bNotIsNull){
				this.ListLyLichSinhVien.Add(oLyLichSinhVien);
			}
		}
		else{
			oLyLichSinhVien=oLoad;
		}
	}
	#region CachedList LyLichSinhVien
	private static List<LyLichSinhVienEntity> _ListLyLichSinhVien=new List<LyLichSinhVienEntity>();
    public List<LyLichSinhVienEntity>ListLyLichSinhVien{
		get{
			return _ListLyLichSinhVien;
		}
	}
	public static List<LyLichSinhVienEntity>ListLyLichSinhVienStatic{
		get{
			return _ListLyLichSinhVien;
		}
	}
	public LyLichSinhVienEntity FindLyLichSinhVien(Int32 SinhVien_ID){
		LyLichSinhVienEntity oResult=this.ListLyLichSinhVien.Find(delegate(LyLichSinhVienEntity oFind){
			bool result=true; 
			result&=oFind.SinhVien_ID.Equals(SinhVien_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLyLichSinhVien(bool clearCache){
		if(_ListLyLichSinhVien.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LyLichSinhVienServiceClient dao = new LyLichSinhVienServiceClient();
		_ListLyLichSinhVien=new List<LyLichSinhVienEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LyLichSinhVienEntity LoadLyLichSinhVien(Int32 SinhVien_ID){			
		LyLichSinhVienEntity o=dao.SelectOne(SinhVien_ID);		
		return o;
	}
	#endregion
}
}
